﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class BaseStateEditParameters : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sVal = Server.UrlDecode(Request.QueryString["Name"].ToString());
            lblParent.Text = "Parameters for " + sVal;
        }

        protected void DefBaseStateGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int iIndex = e.NewEditIndex;

            string sRecID = DefBaseStateGrid.DataKeys[iIndex].Value.ToString();
            string sRecName = DefBaseStateGrid.Rows[iIndex].Cells[2].Text;
            string sRecValue = DefBaseStateGrid.Rows[iIndex].Cells[3].Text;
            if (sRecValue == "&nbsp;" || sRecValue.Trim().Length == 0)
                sRecValue = "NULL";

            string sParentID = Request.QueryString["ID"].ToString();
            string sParentName = Request.QueryString["Name"].ToString();

            Response.Redirect("BaseStateParameterDetail.aspx?Op=Edit&ID=" + sRecID.Trim() + "&Name=" + Server.UrlEncode(sRecName) + "&Value=" + Server.UrlEncode(sRecValue) + "&ParentID=" + sParentID + "&ParentName=" + sParentName);
        }

        protected void DefBaseStateGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int iIndex = e.RowIndex;

            int iParamID = int.Parse(DefBaseStateGrid.DataKeys[iIndex].Value.ToString());

            string sUserID = Request.LogonUserIdentity.Name;
            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                objATBDB.DefBaseStateRemoveParam(iParamID, sUserID);
            }

            // reload page
            Response.Redirect(Request.RawUrl);
        }

        protected void DefBaseStateGrid_Init(object sender, EventArgs e)
        {
            string sVal = Request.QueryString["ID"].ToString();
            int iBaseStateID = Convert.ToInt32(sVal);

            DataTable dtResults;
            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                dtResults = objATBDB.GetDefBaseStateParamList(iBaseStateID);
            }

            DefBaseStateGrid.DataSource = dtResults;
            DefBaseStateGrid.DataBind();
        }

        protected void ActionMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            if (e.Item.Text == "Add Item")
            {
                string sVal = Request.QueryString["ID"].ToString();
                int iBaseStateID = Convert.ToInt32(sVal);
                string sParentName = Request.QueryString["Name"].ToString();

                Response.Redirect("BaseStateParameterDetail.aspx?Op=Add&ParentID=" + iBaseStateID.ToString() + "&ParentName=" + sParentName);
            }
        }
    }
}